function getStyle(obj, attr) {
  return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, false)[attr];
}

// 普通动画函数封装
function startMove(obj, speed, target, attr, endFn) {
  if (obj.timer != null) return false;
  speed = parseInt(getStyle(obj, attr)) < target ? speed : -speed;
  clearInterval(obj.timer);
  obj.timer = setInterval(function() {
    var cur = parseInt(getStyle(obj, attr));
    var len = cur + speed;

    if ((len > target && speed > 0) || (len < target && speed < 0)) len = target;

    obj.style[attr] = len + 'px';
    if (len == target) {
      clearInterval(obj.timer);
      obj.timer = null;
      if (endFn) {
        endFn();
      }
    }
  }, 30);
}

// 抖动动画
function shake(obj, attr, endFn) {
  if (obj.shake != null) return false;
  var objAttr = parseInt(getStyle(obj, attr));
  var arr = [];
  var num = 0;
  for (var i = 20; i > 0; i -= 2) {
    arr.push(i, -i);
  }
  arr.push(0);

  clearInterval(obj.shake);
  obj.shake = setInterval(function() {
    obj.style[attr] = objAttr + arr[num] + 'px';
    num++;
    if (num === arr.length) {
      clearInterval(obj.shake);
      obj.shake = null;
      if (endFn) {
        endFn();
      }
    }
  }, 30);
}

// 透明度动画
function startOpacity(obj, speed, target, endFn) {
  if (obj.opacity != null) return false;

  var cur = 0;
  if (getStyle(obj, 'filter') != 'none') {
    cur = parseInt(getStyle(obj, 'filter').split('=')[1]);
  } else {
    cur = getStyle(obj, 'opacity') * 100;
  }
  speed = cur > target ? -speed : speed;

  clearInterval(obj.opacity);
  obj.opacity = setInterval(function() {
    var cur = 0;
    // 兼容处理
    if (getStyle(obj, 'filter') != 'none') {
      cur = parseInt(getStyle(obj, 'filter').split('=')[1]);
    } else {
      cur = getStyle(obj, 'opacity') * 100;
    }
    var len = cur + speed;
    // 处理边界
    if (len < target && speed < 0 || len > target && speed > 0) {
      len = target;
    }
    // console.log(len);

    obj.style.opacity = len / 100;
    obj.style.filter = 'alpha(opacity=' + len + ')'; // 低版本idea

    if (cur == target) {
      clearInterval(obj.opacity);
      obj.opacity = null;
      if (endFn) {
        endFn();
      }
    }
  }, 30);
}

// 完美动画 ease
function myMove(obj, json, fnEnd) {
  if (obj.timer != null) return false;
  clearInterval(obj.timer);
  obj.timer = setInterval(function() {
    var bStop = true; // 假设所有值已经到达终点
    for (var attr in json) {
      var cur = 0;
      if (attr == 'opacity') {
        if (getStyle(obj, 'filter') != 'none') {
          // 处理兼容性
          cur = parseInt(getStyle(obj, 'filter').split('=')[1]);

          if (isNaN(cur)) {
            obj.style.filter = 'alpha(opacity=100)';
            cur = parseInt(getStyle(obj, 'filter').split('=')[1]);
          }
        } else {
          cur = getStyle(obj, 'opacity') * 100;
        }
      } else {
        cur = parseInt(getStyle(obj, attr));
      }
      var speed = (parseInt(json[attr]) - cur) / 5;
      speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
      if (cur != parseInt(json[attr])) {
        bStop = false;
      }

      if (attr == 'opacity') {
        obj.style.filter = 'alpha(opacity=' + (cur + speed) + ')';
        obj.style.opacity = (cur + speed) / 100;
      } else {
        obj.style[attr] = cur + speed + 'px';
      }
    }

    if (bStop) {
      clearInterval(obj.timer);
      obj.timer = null;
      if (fnEnd) {
        fnEnd();
      }
    }
  }, 30)
}

// 持续时间动画
function move(obj, attr, target, duration, callback) {
  var b = parseFloat(getComputedStyle(obj)[attr]);
  var c = target - b;
  var d = duration;
  var now = new Date().getTime();
  obj[attr] = setInterval(function() {
    var t = new Date().getTime() - now;
    var value = b + c / d * t;
    obj.style[attr] = value + "px";
    if (t >= d) {
      clearInterval(obj[attr]);
      obj.style[attr] = target + "px";
      callback && callback(); //当到达目标值的时候执行回调函数
    }
  }, 30)
}

// 取得现在的时间
function getTime() {
  var now = new Date();
  var YY = now.getFullYear();
  var MM = now.getMonth();
  var DD = now.getDate();
  var WW = now.getDay();

  var h = now.getHours();
  var m = now.getMinutes();
  var s = now.getSeconds();

  return {
    year: YY,
    month: MM,
    day: DD,
    week: WW,
    hours: h,
    minutes: m,
    seconds: s
  };
}

// 一位数转换为两位数,输出字符串
function toTwo(num) {
  return num < 10 ? '0' + num : '' + num;
}

// 月份转换
function switchMonth (n) {
  var arr = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  return arr[n];
}


function getPos(ev) {
  var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  return {
    x: ev.clientX + scrollLeft,
    y: ev.clientY + scrollTop
  };
}
